<?php
declare (strict_types = 1);

namespace app\admin\controller\projects;

use app\admin\controller\AuthController;
use app\admin\model\project\ProWorkdays;
use think\Request;

class WorkDays extends AuthController
{
    /**
     * 显示列表
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function index(Request $req)
    {
        $month = $req->param('month');
        if (empty($month)) {
            $month = date('Y-m');//默认本月
        }
        //该月第一天
        $month_begin = strtotime($month . '-01');
        //最后一天
        $month_end =  strtotime("+1 months -1 day", $month_begin);
        //第一个周一的时间
        $dt_begin = $month_begin - (date('N', $month_begin)-1) * 24*3600;
        //最后一个周末
        $dt_end = $month_end + (7 - date('N', $month_end)) * 24*3600;
        //今天
        $today = strtotime(date('Y-m-d'));

        $list = [];
        $dt = $dt_begin;
        $workdays = ProWorkdays::column('type','day');
        while ($dt <= $dt_end) {
            $class = '';
            if ($dt < $month_begin || $dt > $month_end) {
                $class .= ' other';//不在本月
            }
            if ($dt == $today) {
                $class .= ' today';//今天
            }
            if ($dt > $today && $dt <= $month_end) {
                $class .= ' future';
            }
            if ($dt < $today) {
                $class .= ' old';//今天
            }
            if((date('N', $dt) == 6 && !isset($workdays[$dt])) || (date('N', $dt) != 6 && isset($workdays[$dt])) ) {
                $class .= ' xiu';
                $type = '休';
                $typeclass = $dt < $today ? 'old_xiu' : 'xiu';
                $btn = $dt < $today ? '' : 'set_ban';
            } else {
                $type = '班';
                $typeclass = $dt < $today ? 'old_ban' : 'ban';
                $btn = $dt < $today ? '' : 'set_xiu';
            }
            $list[] = [
                'dt'  => date('Y-m-d', $dt),
                'day' => date('j', $dt),
                'btn' => $btn,
                'type' => $type,
                'typeclass' => $typeclass,
                'class' => $class,
            ];

            $dt += 24*3600;
        }

        $list = array_chunk($list, 7);

        // 月份选择
        $month_val = strtotime($month . '-01');
        $month_cur = date('Y年m月', $month_val);
        $month_pre = date('Y-m', strtotime("-1 months", $month_val));

        $month_next = date('Y-m', strtotime("+1 months", $month_val));

        $data = [
            'month' => $month,
            'list' => $list,
            'month_cur' => $month_cur,
            'month_pre' => $month_pre,
            'month_next' => $month_next,
        ];
        //halt($data);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 创建资源.
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function add(Request $req)
    {
        if($req->isAjax()) {
            $day = strtotime($req->param('day')) ?? 0;
            $type = intval($req->param('type'));
            if($day) {
                if(ProWorkdays::where('day', $day)->find()) {
                    $bool = ProWorkdays::where('day', $day)->delete();
                } else {
                    $bool = ProWorkdays::create(['day'=>$day,'type'=>$type]);
                }
                if($bool) $this->success();
            }
            $this->error();
        }
    }
}
